package jnpf.service.impl;

import jnpf.entity.ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity;
import jnpf.mapper.ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoMapper;
import jnpf.service.ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.util.RandomUtil;

import java.math.BigDecimal;


import jnpf.model.zxgl_xiangmuzhipaixiebanrenzhubiao.ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination;

import java.lang.reflect.Field;

import com.baomidou.mybatisplus.annotation.TableField;

import java.util.stream.Collectors;

import jnpf.entity.ZXGL_XiangMuZhiPaiXieBanRenMingXiBiaoEntity;
import jnpf.service.ZXGL_XiangMuZhiPaiXieBanRenMingXiBiaoService;


import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.text.SimpleDateFormat;

import jnpf.util.*;

import java.util.*;

/**
 * ZXGL_XiangMuZhiPaiXieBanRenZhuBiao
 * 版本： V2.0
 * 版权： 河北交投智能交通技术有限责任公司
 * 作者： 数字交投项目组
 * 日期： 2025-04-10
 */
@Service
public class ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoServiceImpl extends ServiceImpl<ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoMapper, ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity> implements ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoService {


    @Autowired
    private UserProvider userProvider;


    @Autowired
    private ZXGL_XiangMuZhiPaiXieBanRenMingXiBiaoService zXGL_XiangMuZhiPaiXieBanRenMingXiBiaoService;


    @Override
    public List<ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity> getList(ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination) {

        String userId = userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity> zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper = new QueryWrapper<>();
        QueryWrapper<ZXGL_XiangMuZhiPaiXieBanRenMingXiBiaoEntity> zXGL_XiangMuZhiPaiXieBanRenMingXiBiaoQueryWrapper = new QueryWrapper<>();
        int zXGL_XiangMuZhiPaiXieBanRenZhuBiaoNum = 0;
        if (StringUtil.isNotEmpty(zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getDepartmentManagerXzbm())) {
            zXGL_XiangMuZhiPaiXieBanRenZhuBiaoNum++;
            zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper.lambda().eq(ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity::getDepartmentManagerXzbm, zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getDepartmentManagerXzbm());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getCreatorTime())) {
            zXGL_XiangMuZhiPaiXieBanRenZhuBiaoNum++;
            List<String> CreatorTimeList = zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getCreatorTime();
            Long fir = Long.valueOf(CreatorTimeList.get(0));
            Long sec = Long.valueOf(CreatorTimeList.get(1));

            zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper.lambda().ge(ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity::getCreatorTime, new Date(fir))
                    .le(ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity::getCreatorTime, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (AllIdList.size() > 0) {
            zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper.lambda().in(ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity::getId, AllIdList);
        }
        //排序
//        if (StringUtil.isEmpty(zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getSidx())) {
//            zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper.lambda().orderByDesc(ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity::getId);
//        } else {
//            try {
//                String sidx = zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getSidx();
//                ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity zXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity = new ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity();
//                Field declaredField = zXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity.getClass().getDeclaredField(sidx);
//                declaredField.setAccessible(true);
//                String value = declaredField.getAnnotation(TableField.class).value();
//                zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper = "asc".equals(zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getSort().toLowerCase()) ? zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper.orderByAsc(value) : zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper.orderByDesc(value);
//            } catch (NoSuchFieldException e) {
//                e.printStackTrace();
//            }
//        }
        zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper.lambda().orderByDesc(ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity::getCreatorTime);
        if ((total > 0 && AllIdList.size() > 0) || total == 0) {
            Page<ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity> page = new Page<>(zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getCurrentPage(), zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getPageSize());
            IPage<ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity> userIPage = this.page(page, zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper);
            return zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.setData(userIPage.getRecords(), userIPage.getTotal());
        } else {
            List<ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity> list = new ArrayList();
            return zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.setData(list, list.size());
        }
    }

    @Override
    public List<ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity> getTypeList(ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination, String dataType) {
        String userId = userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity> zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper = new QueryWrapper<>();
        QueryWrapper<ZXGL_XiangMuZhiPaiXieBanRenMingXiBiaoEntity> zXGL_XiangMuZhiPaiXieBanRenMingXiBiaoQueryWrapper = new QueryWrapper<>();
        int zXGL_XiangMuZhiPaiXieBanRenZhuBiaoNum = 0;
        if (StringUtil.isNotEmpty(zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getDepartmentManagerXzbm())) {
            zXGL_XiangMuZhiPaiXieBanRenZhuBiaoNum++;
            zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper.lambda().eq(ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity::getDepartmentManagerXzbm, zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getDepartmentManagerXzbm());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getCreatorTime())) {
            zXGL_XiangMuZhiPaiXieBanRenZhuBiaoNum++;
            List<String> CreatorTimeList = zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getCreatorTime();
            Long fir = Long.valueOf(CreatorTimeList.get(0));
            Long sec = Long.valueOf(CreatorTimeList.get(1));

            zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper.lambda().ge(ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity::getCreatorTime, new Date(fir))
                    .le(ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity::getCreatorTime, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (AllIdList.size() > 0) {
            zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper.lambda().in(ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity::getId, AllIdList);
        }
        //排序
        if (StringUtil.isEmpty(zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getSidx())) {
            zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper.lambda().orderByDesc(ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity::getId);
        } else {
            try {
                String sidx = zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getSidx();
                ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity zXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity = new ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity();
                Field declaredField = zXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity.getClass().getDeclaredField(sidx);
                declaredField.setAccessible(true);
                String value = declaredField.getAnnotation(TableField.class).value();
                zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper = "asc".equals(zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getSort().toLowerCase()) ? zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper.orderByAsc(value) : zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper.orderByDesc(value);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        if ("0".equals(dataType)) {
            if ((total > 0 && AllIdList.size() > 0) || total == 0) {
                Page<ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity> page = new Page<>(zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getCurrentPage(), zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.getPageSize());
                IPage<ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity> userIPage = this.page(page, zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper);
                return zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.setData(userIPage.getRecords(), userIPage.getTotal());
            } else {
                List<ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity> list = new ArrayList();
                return zXGL_XiangMuZhiPaiXieBanRenZhuBiaoPagination.setData(list, list.size());
            }
        } else {
            return this.list(zXGL_XiangMuZhiPaiXieBanRenZhuBiaoQueryWrapper);
        }
    }

    @Override
    public ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity getInfo(String id) {
        QueryWrapper<ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity entity) {
        this.save(entity);
    }

    @Override
    public boolean update(String id, ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity entity) {
        entity.setId(id);
        return this.updateById(entity);
    }

    @Override
    public void delete(ZXGL_XiangMuZhiPaiXieBanRenZhuBiaoEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }
    }

    //子表方法
    @Override
    public List<ZXGL_XiangMuZhiPaiXieBanRenMingXiBiaoEntity> GetZXGL_XiangMuZhiPaiXieBanRenMingXiBiaoList(String id) {
        QueryWrapper<ZXGL_XiangMuZhiPaiXieBanRenMingXiBiaoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZXGL_XiangMuZhiPaiXieBanRenMingXiBiaoEntity::getMainid, id);
        return zXGL_XiangMuZhiPaiXieBanRenMingXiBiaoService.list(queryWrapper);
    }

    //列表子表数据方法

}